package com.demboos.android.gtalk.lastsaid;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.util.Log;
import android.widget.RemoteViews;

public class GTalkChatWidget extends AppWidgetProvider {
	
	static final String LOG_TAG = "GalaxySRadioWidget";

	static final ComponentName THIS_APPWIDGET = new ComponentName(
			"com.demboos.android.gtalk.lastsaid",
			"com.demboos.android.gtalk.lastsaid.GTalkChatWidget");
	
	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);

		debug("widgetProvider - onEnabled");

		startService(context);
	}

	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);

		debug("widgetProvider - onDisabled");

		stopService(context);
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);

		debug("widgetProvider - onDeleted");
		
		for (int id : appWidgetIds) {
			sendUnregisterCommand(context, id);
		}
		
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		debug("widgetProvider - onUpdate");

		defaultAppWidget(context, appWidgetIds);
	}
	
	private static void startService(Context context) {
		Intent intent = new Intent(context, GTalkChatService.class);
		context.startService(intent);
	}

	private static void stopService(Context context) {
		Intent intent = new Intent(context, GTalkChatService.class);
		context.stopService(intent);
	}
	
	private static void sendRegisterCommand(Context context, int id) {
		Intent intent = new Intent(context, GTalkChatService.class);
		
		intent.putExtra(GTalkChatService.COMMAND_PARAMETER,
				GTalkChatService.COMMAND_REGISTER);
		intent.putExtra(GTalkChatService.COMMAND_WIDGET_ID_PARAM, id);
		
		
		context.startService(intent);
	}
	
	private static void sendUpCommand(Context context, int id) {
		Intent intent = new Intent(context, GTalkChatService.class);
		
		intent.putExtra(GTalkChatService.COMMAND_PARAMETER, 
				GTalkChatService.COMMAND_UP);	
		intent.putExtra(GTalkChatService.COMMAND_WIDGET_ID_PARAM, id);
		
		context.startService(intent);
	}
	
	private static void sendDownCommand(Context context, int id) {
		Intent intent = new Intent(context, GTalkChatService.class);
		
		intent.putExtra(GTalkChatService.COMMAND_PARAMETER, 
				GTalkChatService.COMMAND_DOWN);
		intent.putExtra(GTalkChatService.COMMAND_WIDGET_ID_PARAM, id);
		
		context.startService(intent);
	}
	
	private static void sendChatCommand(Context context) {
		Intent intent = new Intent(context, GTalkChatService.class);
		
		intent.putExtra(GTalkChatService.COMMAND_PARAMETER, 
				GTalkChatService.COMMAND_CHAT);		
		
		context.startService(intent);
	}
	
	private static void sendUnregisterCommand(Context context, int id) {
		Intent intent = new Intent(context, GTalkChatService.class);
		
		intent.putExtra(GTalkChatService.COMMAND_PARAMETER, 
				GTalkChatService.COMMAND_UNREGISTER);
		intent.putExtra(GTalkChatService.COMMAND_WIDGET_ID_PARAM, id);
		
		context.startService(intent);
	}
	
	public static void defaultAppWidget(Context context, int[] appWidgetIds) {
		// initializing the widget with default layout
		debug("widgetProvider - defaultAppWidget");
		
		startService(context);

		Resources res = context.getResources();
		for (int id : appWidgetIds) {
			RemoteViews views = new RemoteViews(context.getPackageName(),
					R.layout.gtalk_appwidget_2x4);

			pushUpdate(context, new int[] { id }, views);
			
			sendRegisterCommand(context, id);
		}
	}
	
	private static void pushUpdate(Context context, int[] appWidgetIds,
			RemoteViews views) {
		// Update specific list of appWidgetIds if given, otherwise default to
		// all
		debug("widgetProvider - pushUpdate");

		final AppWidgetManager gm = AppWidgetManager.getInstance(context);
		if (appWidgetIds != null) {
			gm.updateAppWidget(appWidgetIds, views);
		} else {
			gm.updateAppWidget(THIS_APPWIDGET, views);
		}
	}
	
	public static void performUpdate(GTalkChatService service, int[] appWidgetIds) {
		final Resources res = service.getResources();

		AppWidgetManager gm = AppWidgetManager.getInstance(service);

		if (appWidgetIds == null) {
			appWidgetIds = gm.getAppWidgetIds(THIS_APPWIDGET);
		}

		for (int id : appWidgetIds) {
			RemoteViews views = new RemoteViews(service.getPackageName(),
					R.layout.gtalk_appwidget_2x4);
			
			views.setTextViewText(R.id.msg_text_1, 
					service.getMessageToDisplay(id, 1).msg);
			views.setTextViewText(R.id.msg_text_2, 
					service.getMessageToDisplay(id, 2).msg);
			views.setTextViewText(R.id.msg_text_3, 
					service.getMessageToDisplay(id, 3).msg);
			views.setTextViewText(R.id.msg_text_4, 
					service.getMessageToDisplay(id, 4).msg);
			
			// Link actions buttons to intents
			linkButtons(service, views);
			
			pushUpdate(service, new int[] { id }, views);
		}
	}
	
	private static void linkButtons(Context context, RemoteViews views) {
		debug("widgetProvider - linkButtons");
		
//		Intent intent = new Intent(GalaxyRadioService.TOGGLEPAUSE_ACTION_INTERNAL);
//		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
//		views.setOnClickPendingIntent(R.id.control_play, pendingIntent);
	}
	
	private static void debug(String msg) {
		Log.d(LOG_TAG, msg);
	}
}
